#!/bin/bash

yum -y install gcc gcc-c++ libstdc++-devel
yum -y install centos-release-scl
  yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
  scl enable devtoolset-9 bash
  echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
yum install tcl


wget http://download.redis.io/releases/redis-6.0.8.tar.gz
tar xzf redis-6.0.8.tar.gz
cd redis-6.0.8/
make

#修改为 后台驻留模式
#sed -i 's/daemonize no/daemonize yes/' /opt/redis-6.0.8/redis.conf
#修改端口
#sed -i 's/port 6379/port 6371/' /opt/redis-6.0.8/redis.conf
#修改密码
#sed -i 's/# requirepass foobared/requirepass NingRedis_123/' /opt/redis-6.0.8/redis.conf

#修改为外部可以访问
sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/' /opt/redis-6.0.8/redis.conf
#还需要修改 保护模式为禁止，不然外网还是不可以访问
sed -i 's/protected-mode yes/protected-mode no/' /opt/redis-6.0.8/redis.conf

/opt/redis-6.0.8/src/redis-server /opt/redis-6.0.8/redis.conf

#set key1 value1
#get key1
#keys 8


#redis 集群模式：主从模式、哨兵模式、Cluster模式
#-------------下面来 Cluster模式-------------------------
mkdir -p /opt/redis_cluster
mkdir -p /opt/redis_cluster/redis_7001
mkdir -p /opt/redis_cluster/redis_7002
mkdir -p /opt/redis_cluster/redis_7003
mkdir -p /opt/redis_cluster/redis_7011
mkdir -p /opt/redis_cluster/redis_7012
mkdir -p /opt/redis_cluster/redis_7013
mkdir -p /opt/redis_cluster/redis_7001/logs/
mkdir -p /opt/redis_cluster/redis_7002/logs/
mkdir -p /opt/redis_cluster/redis_7003/logs/
mkdir -p /opt/redis_cluster/redis_7011/logs/
mkdir -p /opt/redis_cluster/redis_7012/logs/
mkdir -p /opt/redis_cluster/redis_7013/logs/


#----------redis7001-------------------
cat >> /opt/redis_cluster/redis_7001/redis7001.conf <<EOF

# 端口
port 7001
# 启用集群模式
cluster-enabled yes
# 根据你启用的节点来命名，最好和端口保持一致，这个是用来保存其他节点的名称，状态等信息的
cluster-config-file /opt/redis_cluster/redis_7001/nodes_7001.conf
# 超时时间
cluster-node-timeout 5000
appendonly yes
# 后台运行
daemonize yes
# 非保护模式
protected-mode no
pidfile "/opt/redis_cluster/redis_7001/pid/redis_7001.pid"
logfile "/opt/redis_cluster/redis_7001/logs/redis_7001.log"
dbfilename "redis_7001.rdb"

# Generated by CONFIG REWRITE
masterauth "5311ff3a8852488b180817d4f96db324"
requirepass "5311ff3a8852488b180817d4f96db324"
maxclients 9968
maxmemory 1294967296

EOF


#----------redis7002-------------------
cat >> /opt/redis_cluster/redis_7002/redis7002.conf <<EOF

# 端口
port 7002
# 启用集群模式
cluster-enabled yes
# 根据你启用的节点来命名，最好和端口保持一致，这个是用来保存其他节点的名称，状态等信息的
cluster-config-file /opt/redis_cluster/redis_7002/nodes_7002.conf
# 超时时间
cluster-node-timeout 5000
appendonly yes
# 后台运行
daemonize yes
# 非保护模式
protected-mode no
pidfile "/opt/redis_cluster/redis_7002/pid/redis_7002.pid"
logfile "/opt/redis_cluster/redis_7002/logs/redis_7002.log"
dbfilename "redis_7002.rdb"

# Generated by CONFIG REWRITE
masterauth "5311ff3a8852488b180817d4f96db324"
requirepass "5311ff3a8852488b180817d4f96db324"
maxclients 9968
maxmemory 1294967296

EOF

#----------redis7003-------------------
cat >> /opt/redis_cluster/redis_7003/redis7003.conf <<EOF

# 端口
port 7003
# 启用集群模式
cluster-enabled yes
# 根据你启用的节点来命名，最好和端口保持一致，这个是用来保存其他节点的名称，状态等信息的
cluster-config-file /opt/redis_cluster/redis_7003/nodes_7003.conf
# 超时时间
cluster-node-timeout 5000
appendonly yes
# 后台运行
daemonize yes
# 非保护模式
protected-mode no
pidfile "/opt/redis_cluster/redis_7003/pid/redis_7003.pid"
logfile "/opt/redis_cluster/redis_7003/logs/redis_7003.log"
dbfilename "redis_7003.rdb"

# Generated by CONFIG REWRITE
masterauth "5311ff3a8852488b180817d4f96db324"
requirepass "5311ff3a8852488b180817d4f96db324"
maxclients 9968
maxmemory 1294967296

EOF

#----------redis7011-------------------
cat >> /opt/redis_cluster/redis_7011/redis7011.conf <<EOF

# 端口
port 7011
# 启用集群模式
cluster-enabled yes
# 根据你启用的节点来命名，最好和端口保持一致，这个是用来保存其他节点的名称，状态等信息的
cluster-config-file /opt/redis_cluster/redis_7011/nodes_7011.conf
# 超时时间
cluster-node-timeout 5000
appendonly yes
# 后台运行
daemonize yes
# 非保护模式
protected-mode no
pidfile "/opt/redis_cluster/redis_7011/pid/redis_7011.pid"
logfile "/opt/redis_cluster/redis_7011/logs/redis_7011.log"
dbfilename "redis_7011.rdb"

# Generated by CONFIG REWRITE
masterauth "5311ff3a8852488b180817d4f96db324"
requirepass "5311ff3a8852488b180817d4f96db324"
maxclients 9968
maxmemory 1294967296

EOF

#----------redis7012-------------------
cat >> /opt/redis_cluster/redis_7012/redis7012.conf <<EOF

# 端口
port 7012
# 启用集群模式
cluster-enabled yes
# 根据你启用的节点来命名，最好和端口保持一致，这个是用来保存其他节点的名称，状态等信息的
cluster-config-file /opt/redis_cluster/redis_7012/nodes_7012.conf
# 超时时间
cluster-node-timeout 5000
appendonly yes
# 后台运行
daemonize yes
# 非保护模式
protected-mode no
pidfile "/opt/redis_cluster/redis_7012/pid/redis_7012.pid"
logfile "/opt/redis_cluster/redis_7012/logs/redis_7012.log"
dbfilename "redis_7012.rdb"

# Generated by CONFIG REWRITE
masterauth "5311ff3a8852488b180817d4f96db324"
requirepass "5311ff3a8852488b180817d4f96db324"
maxclients 9968
maxmemory 1294967296

EOF

#----------redis7013-------------------
cat >> /opt/redis_cluster/redis_7013/redis7013.conf <<EOF

# 端口
port 7013
# 启用集群模式
cluster-enabled yes
# 根据你启用的节点来命名，最好和端口保持一致，这个是用来保存其他节点的名称，状态等信息的
cluster-config-file /opt/redis_cluster/redis_7013/nodes_7013.conf
# 超时时间
cluster-node-timeout 5000
appendonly yes
# 后台运行
daemonize yes
# 非保护模式
protected-mode no
pidfile "/opt/redis_cluster/redis_7013/pid/redis_7013.pid"
logfile "/opt/redis_cluster/redis_7013/logs/redis_7013.log"
dbfilename "redis_7013.rdb"

# Generated by CONFIG REWRITE
masterauth "5311ff3a8852488b180817d4f96db324"
requirepass "5311ff3a8852488b180817d4f96db324"
maxclients 9968
maxmemory 1294967296

EOF



/opt/redis-6.0.8/src/redis-server /opt/redis_cluster/redis_7001/redis7001.conf
/opt/redis-6.0.8/src/redis-server /opt/redis_cluster/redis_7002/redis7002.conf
/opt/redis-6.0.8/src/redis-server /opt/redis_cluster/redis_7003/redis7003.conf
/opt/redis-6.0.8/src/redis-server /opt/redis_cluster/redis_7011/redis7011.conf
/opt/redis-6.0.8/src/redis-server /opt/redis_cluster/redis_7012/redis7012.conf
/opt/redis-6.0.8/src/redis-server /opt/redis_cluster/redis_7013/redis7013.conf


/opt/redis-6.0.8/src/redis-cli -h 127.0.0.1 -p 7001 -a 5311ff3a8852488b180817d4f96db324 cluster info
/opt/redis-6.0.8/src/redis-cli -h 127.0.0.1 -p 7001 -a 5311ff3a8852488b180817d4f96db324 shutdown
/opt/redis-6.0.8/src/redis-cli -h 127.0.0.1 -p 7002 -a 5311ff3a8852488b180817d4f96db324 shutdown
/opt/redis-6.0.8/src/redis-cli -h 127.0.0.1 -p 7003 -a 5311ff3a8852488b180817d4f96db324 shutdown
/opt/redis-6.0.8/src/redis-cli -h 127.0.0.1 -p 7011 -a 5311ff3a8852488b180817d4f96db324 shutdown
/opt/redis-6.0.8/src/redis-cli -h 127.0.0.1 -p 7012 -a 5311ff3a8852488b180817d4f96db324 shutdown
/opt/redis-6.0.8/src/redis-cli -h 127.0.0.1 -p 7013 -a 5311ff3a8852488b180817d4f96db324 shutdown

# 执行命令
# --cluster-replicas 1 命令的意思是创建master的时候同时创建一个slave

/opt/redis-6.0.8/src/redis-cli --cluster create --cluster-replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7011 127.0.0.1:7012 127.0.0.1:7013 -a 5311ff3a8852488b180817d4f96db324

/opt/redis-6.0.8/src/redis-cli -p 7001 -c -a 5311ff3a8852488b180817d4f96db324
cluster nodes
#set key1 value1
#get key1
#keys 8